<?php
namespace Util\WxRedEnvelope;

use Util\Httpclient;

require_once THINK_PATH . 'Library/Util/WxRedEnvelope/WxPay.Data.php';
require_once THINK_PATH . 'Library/Util/WxRedEnvelope/WxPay.Config.php';
require_once THINK_PATH . 'Library/Util/WxRedEnvelope/WxPay.Exception.php';

class init {
    private $OPENID;
    private $send_name;
    private $total_amount;
    private $total_num;
    private $wishing;
    private $act_name;
    private $remark;
    private $MCHID;
    private $APPID;

    public function __construct() {
        $this->request = new Httpclient();
    }

    /*
     * 设置参数
     * $OPENID 发送红包对象openid
     * $send_name 商户名称
     * $total_amount 付款金额
     * $total_num 红包发放总人数
     * $wishing 红包祝福语
     * $act_name 活动名称
     * $remark 备注
     */
    public function set($OPENID,
                        $send_name = '商户名称',
                        $total_amount = '100',
                        $total_num = 1,
                        $wishing = '提现成功',
                        $act_name = '奇妙阅提现',
                        $remark = '备注',
                        $APPID = 'wx054896dce50a2918',
                        $MCHID = '1490828082') {
        $this->OPENID = $OPENID;
        $this->send_name = $send_name;
        $this->total_amount = $total_amount;
        $this->total_num = $total_num;
        $this->wishing = $wishing;
        $this->act_name = $act_name;
        $this->remark = $remark;
        $this->APPID = $APPID;
        $this->MCHID = $MCHID;
    }

    public function index() {
//        微信红包
        $input = new WxPayRefund();
        $input->Set_HB_mch_billno($this->MCHID . date("YmdHis") . rand(0, 9));
        $input->Set_HB_nonce_str(self::getNonceStr());
        $input->Set_HB_mch_id($this->MCHID);
        $input->Set_HB_wxappid($this->APPID);
        $input->Set_HB_send_name($this->send_name);
        $input->Set_HB_re_openid($this->OPENID);
        $input->Set_HB_total_amount($this->total_amount);
        $input->Set_HB_total_num($this->total_num);

        $input->Set_HB_wishing($this->wishing);
        $input->Set_HB_client_ip(get_client_ip( ));
        $input->Set_HB_act_name($this->act_name);
        $input->Set_HB_remark($this->remark);
        $input->SetSign();
        $XML = $input->ToXml();

        $data = $this->wxHttpsRequestPem("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", $XML);
        return $data;
    }

    /****************************************************
     *  微信带证书提交数据 - 微信红包使用
     ****************************************************/

    public function wxHttpsRequestPem($url, $vars, $second = 30, $aHeader = array()) {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //这里设置代理，如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //以下两种方式需选择一种

        //第一种方法，cert 与 key 分别属于两个.pem文件
        //默认格式为PEM，可以注释
        curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
        curl_setopt($ch, CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH);
        //默认格式为PEM，可以注释
        curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
        curl_setopt($ch, CURLOPT_SSLKEY, WxPayConfig::SSLKEY_PATH);

        curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
        curl_setopt($ch, CURLOPT_CAINFO, WxPayConfig::SSLROOTCA_PATH);

        //第二种方式，两个文件合成一个.pem文件
        //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');

        if (count($aHeader) >= 1) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
        $data = curl_exec($ch);
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }


    public static function getNonceStr($length = 32) {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456719";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
}